var countdown = 60;
var database = require('../../data/data.js');
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: true
    })
    countdown = 60;
    return;
  } else {
    that.setData({
      is_show: false,
      last_time: countdown
    })

    countdown--;
  }
  setTimeout(function () {
    settime(that)
  }
    , 1000)
}

Page({
  data: {
    flag: true,
    last_time: '',
    is_show: true,
    tel: "",
    finish1: false,
    finish2: false,
    finish3: false
  },
  onLoad: function (options) {
    database.flag = false;
  },

  clickVerify: function () {
    var that = this;
    if (that.data.finish1) {
      wx.request({
        url: database.url1 + '/api/V2/user/sms_code',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userphone: database.tel,
          accessToken: database.accessToken
        },
        success(res) {
        },
      })
      that.setData({
        is_show: (!that.data.is_show)
      })
      settime(that);
    }
  },
  tel: function (e) {
    if (e.detail.value.length == 11) {
      database.tel = e.detail.value;
      this.setData({
        finish1: true,
      })
    } else {
      this.setData({
        finish1: false
      })
    }
  },
  code: function (e) {
    if (e.detail.value != "") {
      database.password = e.detail.value.psd
      database.smscode = e.detail.value
      this.setData({
        finish2: true,
      })
    } else {
      this.setData({
        finish2: false
      })
    }
  },
  password: function (e) {
    if (e.detail.value.length >= 6) {
      this.setData({
        finish3: true
      })
    } else {
      this.setData({
        finish3: false
      })
    }
  },
  submit: function (e) {
    wx.showLoading({
      title: '正在绑定',
      mask:true
    })
    var that = this;
    wx.request({
      url: database.url1 + '/api/V2/user/binding_wechat_small',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        username: database.tel,
        accessToken: database.accessToken,
        unionid: database.unionid,
        password: database.password,
        smscode: database.smscode
      },
      success(res) {  
        wx.hideLoading();  
        that.setData({
          flag: false,
          registered_status_text: res.data.status_reason,
          status_flag: res.data.status
        })
      }
    })
  },
  login: function (e) {
    wx.redirectTo({
      url: '/pages/login/login',
    })
  },
  know:function(e){
    this.setData({
      flag: true
    })
    if (this.data.status_flag){
      wx.request({
        url: database.url1 + '/api/V2/user/verify_wechat_small',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          unionid: database.unionid,
          accessToken: database.accessToken
        },
        success(res) {
          if (res.data.data.verify == "no") {
            database.login = false;
          } else {
            database.userId = res.data.data.id;
            database.login = true;
            wx.request({
              url: database.url1 + '/api/V2/user/get_user',
              data: {
                accessToken: database.accessToken,
                userId: database.userId
              },
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success(res) {
                database.tel = res.data.data.mobile;
              }
            })
          }
        }
      })
      wx.redirectTo({
        url: '/pages/home/home',
      })
    }
  }
})